import {
  getWordCloud,
  getMsgTrend,
  getMediaSource,
  getAreaInfo,
  getEmotionInfo,
  getPositiveWord,
  getNegativeWord,
  getOpinionMsg,
} from '@/services/warning';

export default {
  namespace: 'opinionrisk',

  state: {
    wordCloudData: {},
    msgTrendData: {},
    mediaSourceData: {},
    areaInfoData: {},
    emotionInfoData: {},
    positiveWordData: {},
    negativeWordData: {},
    wholeNetMsgData: {},
    weixinMsgData: {},
    weiboMsgData: {},
  },

  effects: {
    *getWordCloudData({ payload }, { call, put }) {
      try {
        const response = yield call(getWordCloud, payload);
        yield put({
          type: 'setWordCloud',
          payload: {
            wordCloudData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *getMsgTrendData({ payload }, { call, put }) {
      try {
        const response = yield call(getMsgTrend, payload);
        yield put({
          type: 'setMsgTrend',
          payload: {
            msgTrendData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *getMediaSourceDate({ payload }, { call, put }) {
      try {
        const response = yield call(getMediaSource, payload);
        yield put({
          type: 'setMediaSource',
          payload: {
            mediaSourceData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *getAreaInfoData({ payload }, { call, put }) {
      try {
        const response = yield call(getAreaInfo, payload);
        yield put({
          type: 'setAreaInfo',
          payload: {
            areaInfoData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *getEmotionInfoData({ payload }, { call, put }) {
      try {
        const response = yield call(getEmotionInfo, payload);
        yield put({
          type: 'setEmotionInfo',
          payload: {
            emotionInfoData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *getPositiveWordData({ payload }, { call, put }) {
      try {
        const response = yield call(getPositiveWord, payload);
        yield put({
          type: 'setPositiveWord',
          payload: {
            positiveWordData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *getNegativeWordData({ payload }, { call, put }) {
      try {
        const response = yield call(getNegativeWord, payload);
        yield put({
          type: 'setNegativeWord',
          payload: {
            negativeWordData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *getWholeNetMsgData({ payload }, { call, put }) {
      try {
        const response = yield call(getOpinionMsg, payload);
        yield put({
          type: 'setWholeNetMsg',
          payload: {
            wholeNetMsgData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *getWeixinMsgData({ payload }, { call, put }) {
      try {
        const response = yield call(getOpinionMsg, payload);
        yield put({
          type: 'setWeixinMsg',
          payload: {
            weixinMsgData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *getWeiboMsgData({ payload }, { call, put }) {
      try {
        const response = yield call(getOpinionMsg, payload);
        yield put({
          type: 'setWeiboMsg',
          payload: {
            weiboMsgData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
  },
  reducers: {
    setWordCloud(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setMsgTrend(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setMediaSource(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setAreaInfo(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setEmotionInfo(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setPositiveWord(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setNegativeWord(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setWholeNetMsg(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setWeixinMsg(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setWeiboMsg(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
